package com.ds.dsm.view.config.menu.service;

import com.ds.common.JDSException;
import com.ds.common.util.StringUtility;
import com.ds.config.ErrorListResultModel;
import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.dsm.view.config.menu.TreeMenuPopTree;
import com.ds.esd.custom.annotation.DialogAnnotation;
import com.ds.esd.custom.annotation.PopTreeViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.bean.nav.tree.NavTreeViewBean;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.form.enums.CustomFormMenu;
import com.ds.esd.custom.form.pop.PopTreeViewBean;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.tree.CustomTreeViewBean;
import com.ds.esd.custom.tree.enums.TreeMenu;
import com.ds.esd.custom.tree.enums.TreeRowMenu;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@RequestMapping(path = "/dsm/view/config/tree/")

public class TreeMenuService {


    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "列表按钮")
    @RequestMapping("TreeMenuTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<TreeMenuPopTree>> getTreeMenuTree(String sourceClassName, String methodName, String viewInstId, String esdsearchpattern) {
        TreeListResultModel<List<TreeMenuPopTree>> model = new TreeListResultModel<>();
        List<TreeMenuPopTree> popTrees = new ArrayList<>();
        try {
            TreeMenuPopTree menuPopTree = new TreeMenuPopTree("treeMenu", "列表按钮");
            for (TreeMenu menu : TreeMenu.values()) {
                TreeMenuPopTree item = new TreeMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("methodName", methodName);
                item.addTagVar("viewInstId", viewInstId);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }


    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "表单按钮")
    @RequestMapping("TreeBottomTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<TreeMenuPopTree>> getMenuBottomTree(String sourceClassName, String methodName, String viewInstId, String esdsearchpattern) {
        TreeListResultModel<List<TreeMenuPopTree>> model = new TreeListResultModel<>();
        List<TreeMenuPopTree> popTrees = new ArrayList<>();
        try {
            TreeMenuPopTree menuPopTree = new TreeMenuPopTree("rowmenu", "列表按钮");
            for (CustomFormMenu menu : CustomFormMenu.values()) {
                TreeMenuPopTree item = new TreeMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("methodName", methodName);
                item.addTagVar("viewInstId", viewInstId);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }


    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "列表按钮")
    @RequestMapping("RowMenuTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<TreeMenuPopTree>> getRowMenuTree(String sourceClassName, String methodName, String domainId, String viewInstId, String esdsearchpattern) {
        TreeListResultModel<List<TreeMenuPopTree>> model = new TreeListResultModel<>();
        List<TreeMenuPopTree> popTrees = new ArrayList<>();
        try {
            TreeMenuPopTree menuPopTree = new TreeMenuPopTree("rowmenu", "列表按钮");
            for (TreeRowMenu menu : TreeRowMenu.values()) {
                TreeMenuPopTree item = new TreeMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("methodName", methodName);
                item.addTagVar("viewInstId", viewInstId);
                item.addTagVar("domainId", domainId);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }


    @RequestMapping("addMenu")
    @APIEventAnnotation(bindMenu = CustomMenuItem.treeSave, callback = {CustomCallBack.ReloadParent, CustomCallBack.Close})
    @ResponseBody
    public ResultModel<Boolean> addMenu(String sourceClassName, String methodName, String TreeMenuTree, String TreeBottomTree, String RowMenuTree, String domainId, String viewInstId) {
        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,  viewInstId);
            MethodConfig customMethodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            CustomTreeViewBean customTreeViewBean = null;
            if (customMethodAPIBean.getView() instanceof CustomTreeViewBean) {
                customTreeViewBean = (CustomTreeViewBean) customMethodAPIBean.getView();
            } else if (customMethodAPIBean.getView() instanceof PopTreeViewBean) {
                PopTreeViewBean baseViewBean = (PopTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = baseViewBean.getTreeViewBean();
            } else if (customMethodAPIBean.getView() instanceof NavTreeViewBean) {
                customTreeViewBean = ((NavTreeViewBean) customMethodAPIBean.getView()).getTreeViewBean();
            }
            if (TreeMenuTree != null && !TreeMenuTree.equals("")) {
                String[] menuIds = StringUtility.split(TreeMenuTree, ";");
                for (String menuId : menuIds) {
                    if (menuId != null && !menuIds.equals("")) {
                        customTreeViewBean.getCustomMenu().add(TreeMenu.valueOf(menuId));
                    }
                }
            } else if (RowMenuTree != null && !RowMenuTree.equals("")) {
                String[] menuIds = StringUtility.split(RowMenuTree, ";");
                for (String menuId : menuIds) {
                    if (menuId != null && !menuIds.equals("")) {
                        customTreeViewBean.getRowCmdBean().getRowMenu().add(TreeRowMenu.valueOf(menuId));
                    }
                }
            } else if (TreeBottomTree != null && !TreeBottomTree.equals("")) {
                String[] menuIds = StringUtility.split(TreeBottomTree, ";");
                for (String menuId : menuIds) {
                    if (menuId != null && !menuIds.equals("")) {
                        customTreeViewBean.getBottombarMenu().add(CustomFormMenu.valueOf(menuId));
                    }
                }
            }

            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(tableConfig);
        } catch (
                JDSException e)

        {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }


        return model;
    }


    @RequestMapping("delMenu")
    @APIEventAnnotation(bindMenu = CustomMenuItem.delete, callback = {CustomCallBack.Reload})
    @ResponseBody
    public ResultModel<Boolean> delMenu(String sourceClassName, String methodName, String type, String domainId, String viewInstId) {
        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig customMethodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            CustomTreeViewBean customTreeViewBean = null;
            if (customMethodAPIBean.getView() instanceof CustomTreeViewBean) {
                customTreeViewBean = (CustomTreeViewBean) customMethodAPIBean.getView();
            } else if (customMethodAPIBean.getView() instanceof PopTreeViewBean) {
                PopTreeViewBean baseViewBean = (PopTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = baseViewBean.getTreeViewBean();
            } else if (customMethodAPIBean.getView() instanceof NavTreeViewBean) {
                customTreeViewBean = ((NavTreeViewBean) customMethodAPIBean.getView()).getTreeViewBean();
            }
            String[] menuIds = StringUtility.split(type, ";");
            for (String menuId : menuIds) {
                TreeMenu treeMenu = TreeMenu.fromType(menuId);
                if (treeMenu != null) {
                    customTreeViewBean.getCustomMenu().remove(TreeMenu.valueOf(menuId));
                }
                TreeRowMenu rowMenu = TreeRowMenu.fromType(menuId);
                if (rowMenu != null) {
                    customTreeViewBean.getRowCmdBean().getRowMenu().remove(TreeRowMenu.valueOf(menuId));

                }

            }
            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(tableConfig);
        } catch (JDSException e) {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }
        return model;
    }


}
